package com.ruoyi.gd.cgbjd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.ldjl.domain.ZLdjl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.cgbjd.domain.bo.ZCgbjdBo;
import com.ruoyi.gd.cgbjd.domain.vo.ZCgbjdVo;
import com.ruoyi.gd.cgbjd.domain.ZCgbjd;
import com.ruoyi.gd.cgbjd.mapper.ZCgbjdMapper;
import com.ruoyi.gd.cgbjd.service.IZCgbjdService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 常规报价单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-29
 */
@RequiredArgsConstructor
@Service
public class ZCgbjdServiceImpl implements IZCgbjdService {

    private final ZCgbjdMapper baseMapper;

    /**
     * 查询常规报价单
     */
    @Override
    public ZCgbjdVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询常规报价单列表
     */
    @Override
    public TableDataInfo<ZCgbjdVo> queryPageList(ZCgbjdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZCgbjd> lqw = buildQueryWrapper(bo);
        Page<ZCgbjdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询常规报价单列表
     */
    @Override
    public List<ZCgbjdVo> queryList(ZCgbjdBo bo) {
        LambdaQueryWrapper<ZCgbjd> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZCgbjd> buildQueryWrapper(ZCgbjdBo bo) {
        Map<String, Object> params = bo.getParams();
        LoginUser loginUser = LoginHelper.getLoginUser();
        LambdaQueryWrapper<ZCgbjd> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getXh()), ZCgbjd::getXh, bo.getXh());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZCgbjd::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDw()), ZCgbjd::getDw, bo.getDw());
        lqw.eq(bo.getDj() != null, ZCgbjd::getDj, bo.getDj());
        lqw.like(StringUtils.isNotBlank(bo.getBz()), ZCgbjd::getBz, bo.getBz());
        // 管理员可以查看所有的数据
        if(!"admin".equals(loginUser.getUsername())) {
            List<String> strings = Arrays.asList("0");
            lqw.in(ZCgbjd::getDelFlag, strings);
        }
        return lqw;
    }

    /**
     * 新增常规报价单
     */
    @Override
    public Boolean insertByBo(ZCgbjdBo bo) {
        ZCgbjd add = BeanUtil.toBean(bo, ZCgbjd.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改常规报价单
     */
    @Override
    public Boolean updateByBo(ZCgbjdBo bo) {
        ZCgbjd update = BeanUtil.toBean(bo, ZCgbjd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZCgbjd entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除常规报价单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.batchDeleteIds(ids) > 0;
    }
}
